package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.service.admin.MarketCouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class MarketCouponServiceImpl implements MarketCouponService {

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    /**
     * 显示优惠券内容，兼顾查询
     * @param name,type,status,basePageInfo
     * @return CommonData
     */
    @Override
    public CommonData<MarketCoupon> list(String name, Short type, Short status, BasePageInfo basePageInfo) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            // 模糊查询需要出去空格
            name = "%" + name.trim() + "%";
            criteria.andNameLike(name);
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);

        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);
        PageInfo<MarketCoupon> marketCouponPageInfo = new PageInfo<>(marketCoupons);
        CommonData data = CommonData.data(marketCouponPageInfo);

        return data;
    }

    /**
     * 添加优惠券
     * po接收即可,依旧是多了id, addTime, updateTime三项
     * id需要接收，后两项手动初始值相同
     * @param marketCoupon
     * @return marketCoupon
     */
    @Override
    public MarketCoupon create(MarketCoupon marketCoupon) {
        Date date = new Date();
        marketCoupon.setAddTime(date);
        marketCoupon.setUpdateTime(date);
        int insertSelective = marketCouponMapper.insertSelective(marketCoupon);
        return marketCoupon;
    }

    /**
     * 显示优惠券详情
     * @param id
     */
    @Override
    public MarketCoupon read(Integer id) {
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();

        criteria.andIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<MarketCoupon> marketCouponList = marketCouponMapper.selectByExample(marketCouponExample);

        if (marketCouponList.size() != 1) {
            return null;
        }

        MarketCoupon marketCoupon = marketCouponList.get(0);
        return marketCoupon;
    }

    /**
     * 优惠券详情页显示用户
     * @param couponId,userId,status,basePageInfo
     * couponId必定有值
     */
    @Override
    public CommonData<MarketCouponUser> listUser(Integer couponId, Integer userId, Short status, BasePageInfo basePageInfo) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.createCriteria();

        criteria.andCouponIdEqualTo(couponId);
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);

        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(marketCouponUserExample);
        for (MarketCouponUser marketCouponUser : marketCouponUsers) {
            if (marketCouponUser.getStatus() == 2) {
                marketCouponUser.setStatus((short) 3);
            } else if (marketCouponUser.getStatus() == 3) {
                marketCouponUser.setStatus((short) 2);
            }
        }
        PageInfo<MarketCouponUser> marketCouponUserPageInfo = new PageInfo<>(marketCouponUsers);
        return CommonData.data(marketCouponUserPageInfo);
    }

    /**
     * 更新优惠券信息，只需要修改updateTime
     * @param marketCoupon
     * @return marketCoupon
     */
    @Override
    public MarketCoupon update(MarketCoupon marketCoupon) {
        marketCoupon.setUpdateTime(new Date());
        int update = marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
        return marketCoupon;
    }

    /**
     * 删除优惠券
     * @param marketCoupon
     */
    @Override
    public int delete(MarketCoupon marketCoupon) {
        marketCoupon.setDeleted(true);
        int update = marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
        // todo 是否要删除持有该优惠券的用户
        /*MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.createCriteria();

        criteria.andCouponIdEqualTo(marketCoupon.getId());
        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setDeleted(true);

        int upd = marketCouponUserMapper.updateByExampleSelective(marketCouponUser, marketCouponUserExample);*/

        return update;
    }
}
